package com.webBasic.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.webBasic.Util.GyUtil;
import com.webBasic.domain.Student;
import com.webBasic.service.StudentService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 王凯飞
 * @date:2019/10/29 学生信息
 */
@Controller
@RequestMapping("/student")
public class StudentController {
    public final Log logger = LogFactory.getLog(this.getClass());
    @Autowired
    private StudentService studentService;


    /**
     * 学生列表
     * 分页暂不考虑
     */
    @RequestMapping(value = "/queryAllStudent")
    public void queryAllStudent(HttpServletResponse resp) {
        try {
            /*list集合中存放的是好多student对象*/
            List<Student> studentList = studentService.selectStudentList();
            /*将list集合装换成json对象*/
            List<Object> students=new ArrayList<>();
            students.addAll(studentList);
            JSONArray data=new JSONArray(students);
            //接下来发送数据
            /*设置编码，防止出现乱码问题*/
            resp.setCharacterEncoding("utf-8");
            /*得到输出流*/
            PrintWriter respWritter = resp.getWriter();
            /*将JSON格式的对象toString()后发送*/
            respWritter.append(data.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 学生列表
     * 分页暂不考虑
     */
    @RequestMapping(value = "/queryAllStudent1", method = RequestMethod.POST)
    public void queryAllStudent1(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        try {
            JSONObject jsonObject = new JSONObject();
            /*list集合中存放的是好多student对象*/
            List<Student> studentList = studentService.selectStudentList();
            JSONArray array = new JSONArray();
            jsonObject.put("resultList", array);
            String[] cloumns=   new String[]{"id", "name","grade","age"};
            for(int i = 0; i < studentList.size(); ++i) {
                JSONObject json = new JSONObject();
                Student student=studentList.get(i);
                String[] var8 = cloumns;
                for(int var10 = 0; var10 < cloumns.length; ++var10) {
                    String key = var8[var10];
                    json.put(key, GyUtil.invokeGetterMethod(student, key) );
                }
                array.add(json);
            }
            GyUtil.ajaxPageResult(jsonObject, response);
        } catch (Exception ex) {
            String msg = "查询学生列表时发生异常：" + ex.getMessage();
            logger.error(msg, ex);
        }
    }

    /**
     * 学生列表
     * 分页暂不考虑
     */
    @RequestMapping(value = "/selectStudentList",method = RequestMethod.GET)
    @ResponseBody
    public JSONObject selectStudentList() {
        // 返回结果
        JSONObject json = new JSONObject();
        List<Student> studentList = studentService.selectStudentList();
        json.put("data", studentList);
        json.put("code", 0);
        json.put("msg", "查询学生列表成功！");
        return json;
    }

    /**
     * 学生详细信息
     */
    @RequestMapping(value = "/selectStudentById",method = RequestMethod.GET)
    @ResponseBody
    public JSONObject  selectStudentById(@Param("id") Integer id, @Param("status") Integer status) {
        // 返回结果
        JSONObject json = new JSONObject();
        Student student = studentService.selectStudentById(id,status);
        json.put("data", student);
        json.put("code", 0);
        json.put("msg", "查询学生详细信息成功！");
        return json;
    }

}
